﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Domian.Core;
using Domian.Core.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ImagePost.Controllers
{
    [Route("api/Image")]
    [ApiController]
    public class ImageController : ControllerBase
    {
        private readonly IPostImageService _postImageService;
        public ImageController(IPostImageService postImageService)
        {
            this._postImageService = postImageService;
        }

        [HttpGet, Route("Index")]
        public void Index()
        {

        }


        [HttpPost, Route("Post")]
        public CommonResult post()
        {
            Stream stream = HttpContext.Request.Body;
            byte[] buffer = new byte[HttpContext.Request.ContentLength.Value];
            stream.Read(buffer, 0, buffer.Length);
            string str = Encoding.UTF8.GetString(buffer);
            string base64String = HttpUtility.UrlDecode(str, Encoding.UTF8); ;
            return this._postImageService.saveImage(base64String);
        }
    }
}